<template>
  <div class="exam-list-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的考试</span>
          <el-button type="primary" @click="fetchExamList">
            刷新
          </el-button>
        </div>
      </template>
      
      <el-table
        v-loading="loading"
        :data="examList"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="paperName" label="试卷名称" min-width="200" />
        <el-table-column label="开始时间" width="180">
          <template #default="{ row }">
            {{ formatDateTime(row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" width="180">
          <template #default="{ row }">
            {{ formatDateTime(row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="duration" label="时长（分钟）" width="120" />
        <el-table-column label="状态" width="120">
          <template #default="{ row }">
            <el-tag v-if="row.examStatus === 'not_started'" type="info">未开始</el-tag>
            <el-tag v-else-if="row.examStatus === 'in_progress'" type="warning">进行中</el-tag>
            <el-tag v-else-if="row.examStatus === 'finished'" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button
              v-if="row.examStatus === 'not_started'"
              type="primary"
              size="small"
              @click="handleStartExam(row.paperId)"
            >
              开始考试
            </el-button>
            <el-button
              v-else-if="row.examStatus === 'in_progress'"
              type="warning"
              size="small"
              @click="handleContinueExam(row.examId)"
            >
              继续考试
            </el-button>
            <el-button
              v-else-if="row.examStatus === 'finished'"
              type="success"
              size="small"
              @click="handleViewResult(row.examId)"
            >
              查看结果
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getAvailableExams, type AvailableExam } from '../../api/exam'

const router = useRouter()
const loading = ref(false)
const examList = ref<AvailableExam[]>([])

// 获取考试列表
const fetchExamList = async () => {
  loading.value = true
  try {
    const res = await getAvailableExams()
    examList.value = res.data || []
  } catch (error) {
    ElMessage.error('获取考试列表失败')
  } finally {
    loading.value = false
  }
}

// 开始考试
const handleStartExam = async (paperId: number) => {
  try {
    router.push({
      name: 'ExamTaking',
      params: { paperId: paperId.toString() }
    })
  } catch (error) {
    ElMessage.error('开始考试失败')
  }
}

// 继续考试
const handleContinueExam = (examId: number) => {
  router.push({
    name: 'ExamTaking',
    params: { examId: examId.toString() }
  })
}

// 查看结果
const handleViewResult = (examId: number) => {
  router.push({
    name: 'ExamResult',
    params: { examId: examId.toString() }
  })
}

// 格式化日期时间
const formatDateTime = (dateTimeStr: string) => {
  if (!dateTimeStr) return ''
  const date = new Date(dateTimeStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  fetchExamList()
})
</script>

<style scoped>
.exam-list-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>


